﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace neside3
{
    public static partial class IDE
    {
        public static class Builder
        {
            static GroupBox area_result;
            public static void Init(GroupBox _area_result)
            {
                Builder.area_result = _area_result;
            }
            public static bool HasBuildTask
            {
                get;
                private set;
            }
            public async static Task<bool> Build()
            {
                if (HasBuildTask)
                {
                    IDE.logger.Error("正在进行一个Builder任务，不可同时执行");
                    return false;
                }
                HasBuildTask = true;
                var folder = IDE.curProject_Folder;
                IDE.ProjOption opt = null;
                try
                {
                    opt = GetCurProject_Option();
                }
                catch (System.Exception err)
                {
                    IDE.logger.Error("項目配置有错误:" + err.Message);
                    HasBuildTask = false;
                    return false;
                }
                if (opt == null)
                {
                    IDE.logger.Error("項目配置未找到 neside.json");
                    HasBuildTask = false;
                    return false;
                }
                if (IDE.plugin_builder.TryGetValue(opt.buildtype, out ext.IBuilder builder))
                {
                    builder.SetProject(IDE.logger, folder);
                    var b = await builder.Build(IDE.logger, opt.buildoption);
                    if (b)
                        IDE.logger.Warn("build succ.");
                    else
                        IDE.logger.Warn("build fail.");

                    HasBuildTask = false;
                    return b;
                }
                IDE.logger.Warn("没有对应的builder:" + opt.buildtype);
                HasBuildTask = false;
                return false;
            }
        }
    }
}
